/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.Penerimaan;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author YosephDewa
 */
@WebServlet(urlPatterns = {"/FilterSimpanTerimaGasLPG"})
public class FilterSimpanTerimaGasLPG extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Penerimaan penerimaan = new Penerimaan();
        String jumlah = request.getParameter("jumlah");
        String jenis = request.getParameter("pilih");
        String harga = request.getParameter("harga");
        if ("".equals(harga) || "".equals(jumlah) || "[0-9]*".equals(harga) || "[0-9]*".equals(jumlah)) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("TerimaGasLPG.jsp");
            requestDispatcher.forward(request, response);
        } else {
            penerimaan.setJumlahGasElpiji(Double.parseDouble(jumlah));
            penerimaan.setJenisGasElpiji(jenis);
            penerimaan.setHargaGasElpiji(Double.parseDouble(harga));
            Cookie[] ck = request.getCookies();
            penerimaan.setUsername(ck[1].getValue());
            penerimaan.insertPenerimaan();
            out.println("<head>"
                    + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
                    + "<title>Data Tersimpan</title></head>");
            out.println("<body bgcolor=\"#CCCCCC\">"
                    + "<table border=\"0\" align=\"center\"><tr>"
                    + "<td align=\"center\">"
                    + "<font style=\"font-size: 24px ; font-style: Arial ; color: background\" >FORM TERIMA GAS LPG</font><br>"
                    + "<font style=\"font-size: 14px ; font-style: Arial ; color: background\" >PT. KEBON ARUM GROUP</font>"
                    + "<br><br><h1 style=\"font-size: x-large\">DATA TERSIMPAN</h1>"
                    + "</td></tr></tabl>");
            out.println("<form action=\"MenuUtamaAdmin.jsp\">"
                    + "<table align=\"center\" border=\"0\">");
            out.println("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"kembali\" value=\"kembali ke form utama\" width=\"250\"/></td>"
                    + "</tr></table></form>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
